package org.example.likou150.hash;

import java.util.HashMap;

public class WordPattern {
    public static void main(String[] args) {
        WordPattern wordPattern=new WordPattern();
        System.out.println(wordPattern.wordPattern("abba", "dog cat cat dog"));
    }
    public boolean wordPattern(String pattern, String s) {
        char[] patternCharArray = pattern.toCharArray();
        String[] sArray = s.split(" ");
        if(patternCharArray.length != sArray.length) return false;
        String[] sBloom=new String[128];
        HashMap<String, Character> hashMap=new HashMap<>();
        for (int i = 0; i < patternCharArray.length; i++) {
            if((sBloom[patternCharArray[i]]!=null&&!sBloom[patternCharArray[i]].equals(sArray[i]))
                    ||
               (hashMap.get(sArray[i])!=null && hashMap.get(sArray[i])!=patternCharArray[i])){
                return false;
            }
            sBloom[patternCharArray[i]]=sArray[i];
            hashMap.put(sArray[i], patternCharArray[i]);
        }
        return true;
    }
}
